package com.learn.jdk.dataapi;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.concurrent.TimeUnit;

/**
 * @author: lipan
 * @date: 2021/5/19 21:20
 */

public class Demo2 {

//    public static void main(String[] args) {
//
//        testLocalDate();
//
//        testLocalTime();
//
//    }

    //日期
    private  static void   testLocalDate(){
        //获取当前日期
        LocalDate now = LocalDate.now();
        System.out.println(now);

        System.out.println("年份:"+now.getYear());
        System.out.println("月份:"+now.getMonth());
        System.out.println("哪一天:"+now.getDayOfYear());


        //自定义日期
        LocalDate of = LocalDate.of(1997, 7, 1);
        System.out.println(of);

        System.out.println("年份:"+of.getYear());
        System.out.println("月份:"+of.getMonth());
        System.out.println("哪一天:"+of.getDayOfYear());

    }

    //时间
    private static  void testLocalTime(){

        LocalTime now = LocalTime.now();

        System.out.println("现在时间是:"+now);

        System.out.println("时:"+now.getHour());
        System.out.println("分:"+now.getMinute());
        System.out.println("秒:"+now.getSecond());

        LocalTime of = LocalTime.of(19, 30, 15);
        System.out.println(of);


    }




    @Test
    public   void testLocalDateTime(){

       LocalDateTime now= getNowDateTime();

        System.out.println(now);

    }

    private LocalDateTime getNowDateTime() {

        LocalDate today=LocalDate.now();

        LocalTime time=LocalTime.now();

        LocalDateTime of = LocalDateTime.of(today, time);

        return  of;
    }


    /**
     * 计算两个之间相隔毫秒数
     * @throws Exception
     */
    @Test
    public void testTimeDuration() throws Exception{

        //当前时间
        Instant before = Instant.now();

        doBusi();

        Instant after = Instant.now();

        System.out.println(Duration.between(before,after).toMillis());

    }


    @Test
    public void testTime() throws Exception{
        LocalDate today=LocalDate.of(2014,9,1);

        LocalTime time=LocalTime.of(10,10,10);

        LocalDateTime dateTime1 = LocalDateTime.of(today, time);

        LocalDate date=LocalDate.of(2021,9,1);

        LocalTime time2=LocalTime.of(10,10,10);

        LocalDateTime dateTime2 = LocalDateTime.of(date, time2);

        System.out.println(Duration.between(dateTime1,dateTime2).toHours());


    }


    //日期转字符串
    @Test
    public void testFormat() throws Exception{

        LocalDateTime nowDateTime = getNowDateTime();

        //系统自带的转换

        System.out.println(nowDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));

        //自定义

        System.out.println(nowDateTime.format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss")));

    }


    //日期转字符串
    @Test
    public void testParse() throws Exception{

        String timeStr="2021/10/03 22:19:11";

        System.out.println(LocalDateTime.parse(timeStr, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss")));

    }

    private void doBusi() {

        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
